目標:有後台系統可以查詢自己的文章,並做CRUD。
第一步 : routes
namespace :account do
resources :articles
end
** 第二步:建立controller**rails g controller account/articles
(因為之前已經有建立的article的Model ,這裡不需要再建立)
class Account::ArticlesController < ApplicationController
before_action :authenticate_user!
def index
@articles = current_user.articles.order("position ASC")
end
end
** 第三步 建立account/articles/index.html.erb**touch app/views/account/articles/index.html.erb
<h3 class="text-center">文章列表(Article List)</h3>
<br>
<div>
<%= link_to("增加文章", new_article_path, class: "btn btn-sm btn-default pull-right") %>
</div>
<!-- 所有文章列表 -->
<table class="table table-hover table-bordered table-responsive">
<thead>
<tr>
<td>標題</td>
<td>發表時間</td>
<td>操作</td>
<td>狀態</td>
<td>TAGS</td>
</tr>
</thead>
<tbody>
<% @articles.each do |article| %>
<tr>
<td><%= link_to(article.title, article) %></td>
<td><%= article.created_at %></td>
<td>
<%= link_to("Edit", edit_article_path(article)) %> |
<%= link_to("destroy", article_path(article), method: :delete, data: { confirm: "你确定要删除吗?"}) %>
</td>
<td>
<% if article.is_hidden %>
<i class="fa fa-eye"> 審核中 </i>
<% else %>
<i class="fa fa-eye-slash"> 已通過審核</i>
<% end %>
</td>
<td><%= raw article.tags.map(&:name).map { |t| link_to t, tag_path(t) }.join(', ') %></td>
</tr>
<% end %>
</tbody>
</table>
第四步 建立account後台layout區別
layout "account"
touch app/views/layouts/account.html.erb
<!DOCTYPE html>
<html>
<head>
<title>個人中心</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<div class="content">
<div class="container">
<%= render "common/navbar" %>
<%= render "common/flashes" %>
<div class="">
<!-- 側邊分類 -->
<div class="col-md-2 nav-left-style">
<ul class="nav nav-pills nav-stacked" style="max-width: 300px;">
<hr>
<li><%= link_to("文章管理", account_articles_path) %></li>
</ul>
</div>
<!-- 主内容 -->
<div class="col-md-10">
<%= yield %>
</div>
</div>
</div>
</div>
<%= render "common/footer" %>
</body>
</html>
<%=link_to("個人中心", account_articles_path) %>